package org.datamining.guha.association;

import java.util.List;
import java.util.ArrayList;

import org.datamining.guha.model.PatternTreeModel;
import org.datamining.guha.model.literal.Literal;

//import org.apache.commons.logging.*;
        
/**
 * Funded association. Also know as Simple association.
 * See [Hajek]
 *
 * @author Lukas Vlcek
 */
public class FundedAssociation extends AbstractAssociation {
    
    /** Creates a new instance of FundedAssociation */
    public FundedAssociation(double support, double confidence) {
        super(support, confidence);
    }
    
    String associationDescription() {
        return "Funded Association";
    }
    
    void ruleEvalutaion(PatternTreeModel ptm, int numberOfObjects) {
        
        List<Literal> notAnte = getNegatedVersion(antecedent);
        List<Literal> notSucc = getNegatedVersion(succedent);
        
        if (log.isDebugEnabled()) {
            log.debug("antecedent " + antecedent);
            log.debug("succedent " + succedent);
            log.debug("notAntecedent " + notAnte);
            log.debug("notSuccedent " + notSucc);
        }
        
        List<Literal> fullCedent = new ArrayList<Literal>();
        // A
        fullCedent.addAll(antecedent);
        fullCedent.addAll(succedent);
        long suppA = ptm.getPatternSupport(fullCedent);
        
        // D
        fullCedent.clear();
        fullCedent.addAll(notAnte);
        fullCedent.addAll(notSucc);
        long suppD = ptm.getPatternSupport(fullCedent);
        
        // B
        fullCedent.clear();
        fullCedent.addAll(antecedent);
        fullCedent.addAll(notSucc);
        long suppB = ptm.getPatternSupport(fullCedent);
        
        // C
        fullCedent.clear();
        fullCedent.addAll(notAnte);
        fullCedent.addAll(succedent);
        long suppC = ptm.getPatternSupport(fullCedent);
        
        /*
         * ad > bc must hold.
         *
         * Thus we define confidence the following way:
         * Let given conf. threshold be C, then
         * confidence is C + (ad - bc).
         */
        ruleConfidence = (double) confidence * (suppA*suppD - suppB*suppC);
        ruleSupport = (double) suppA/numberOfObjects;
        
        /*
        log.debug("Testing hypothesis: " + antecedent + " => " + succedent);
        log.debug("supp = " + ruleSupport);
        log.debug("conf = " + ruleConfidence);
        log.debug(" - - - - - - ");
        */
    }
}
